﻿using DataManageSystem.Common;
using DataManageSystem.Data;
using DataManageSystem.Exceptions;
using DataManageSystem.Models;
using DataManageSystem.Models.Dto;
using DataManageSystem.Services;
using DataManageSystem.Utils;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace DataManageSystem.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class OfficeDevCategoryController : ControllerBase
    {
        private readonly OfficeDevCategoryServices _officeDevCategoryServices;

        public OfficeDevCategoryController(OfficeDevCategoryServices officeDevCategoryServices)
        {
            _officeDevCategoryServices = officeDevCategoryServices;
        }

        [HttpGet("selectCateGoryData")]
        public ObjectResult selectNoticeData(
            [FromQuery] int pageNum = 1,
            [FromQuery] int pageSize = 10,
            [FromQuery] string categoryName = ""
        )
        {
            var res = _officeDevCategoryServices.selectCateGoryData(pageNum, pageSize, categoryName);
            return new ObjectResult(Result.Success(res));
        }

        [HttpGet("getAllCategories")]
        public ObjectResult GetAllCategories()
        {
            var categories = _officeDevCategoryServices.getAllCategories(); 
            return new ObjectResult(Result.Success(categories));
        }



        [HttpPost("add")]
        public ObjectResult addCateGory([FromBody] OfficeDevCategory devCategory)
        {
            _officeDevCategoryServices.add(devCategory);
            return new ObjectResult(Result.Success("添加成功"));
        }

        [HttpPut("update")]
        public ObjectResult Update([FromBody] OfficeDevCategory devCategory)
        {
            _officeDevCategoryServices.Update(devCategory);
            return new ObjectResult(Result.Success("设备信息更新成功"));
        }

        [HttpDelete("delete/{id}")]
        public ObjectResult Delete(int id)
        {
            try
            {
                _officeDevCategoryServices.Delete(id);
                return new ObjectResult(Result.Success("删除成功"));
            }
            catch (ServiceException ex)
            {
                return new ObjectResult(Result.Error(ex.Message));
            }
            catch (Exception ex)
            {
                return new ObjectResult(Result.Error(ex.Message));
            }
        }


    }
}
